001 package videoautomat; 002 import javax.swing.table.TableCellRenderer; 003 004 import util.swing.AbstractTableEntryDescriptor; 005 import data.NumberValue; 006 import data.QuoteValue; 007 import data.swing.CountingStockTableModel; 008 import data.swing.CurrencyRenderer; 009 010 /** 011 * This class implements a TableEntryDescriptor used to display the {@link VideoShop#getVideoStock()} 012 */ 013 public class TEDVideoStock extends AbstractTableEntryDescriptor { 014 015 private String[] cNames = { "Name", "Price", "Count" }; 016 private Class[] cClasses = { String.class, NumberValue.class, Integer.class }; 017 CurrencyRenderer renderer = new CurrencyRenderer(VideoShop.getCurrency()); 018 /** 019 * @return the number of columns each record will consist of. 020 * 021 * @see util.swing.TableEntryDescriptor#getColumnCount() 022 */ 023 public int getColumnCount() { 024 return 3; 025 } 026 027 /** 028 * @return the text to be printed in the header of the given column. 029 * @param nIdx 030 * the index of the column for which to return the header. Indices run from 0 to 031 * {@link TEDVideoStock#getColumnCount()}- 1. 032 * @see util.swing.TableEntryDescriptor#getColumnName(int) 033 */ 034 public String getColumnName(int nIdx) { 035 return cNames[nIdx]; 036 } 037 038 /** 039 * @return the class of objects that make up the values of cells of the given column. This will be used to 040 * determine the cell renderer and editor unless you specify otherwise through 041 * util.swing.AbstractTableEntryDescriptor#getCellEditor(int) and 042 * util.swing.AbstractTableEntryDescriptor#getCellRenderer(int). 043 * @param nIdx 044 * the index of the column for which to return the header. Indices run from 0 to 045 * {@link TEDVideoStock#getColumnCount()}- 1. 046 * @see util.swing.TableEntryDescriptor#getColumnClass(int) 047 */ 048 public Class getColumnClass(int nIdx) { 049 return cClasses[nIdx]; 050 } 051 052 /** 053 * @return the value to be printed in the given column for the given record. The actual class must be a subclass of 054 * what was returned by {@link TEDVideoStock#getColumnClass(int)}or that class itself. 055 * @param nIdx 056 * the index of the column for which to return the header. Indices run from 0 to 057 * {@link TEDVideoStock#getColumnCount()}- 1. 058 * @see util.swing.TableEntryDescriptor#getValueAt(java.lang.Object, int) 059 */ 060 public Object getValueAt(Object oRecord, int nIdx) { 061 CountingStockTableModel.Record r = (CountingStockTableModel.Record) oRecord; 062 switch (nIdx) { 063 case 0 : 064 return r.getDescriptor().getName(); 065 case 1 : 066 return ((QuoteValue) r.getDescriptor().getValue()).getOffer(); 067 case 2 : 068 return new Integer(r.getCount()); 069 } 070 return null; 071 } 072 073 /** 074 * @return the cell renderer to be used for cells in the given column. 075 * @param nIdx 076 * the index of the column for which to return the header. Indices run from 0 to 077 * {@link TEDVideoStock#getColumnCount()}- 1. 078 * @see util.swing.TableEntryDescriptor#getCellRenderer(int) 079 */ 080 public TableCellRenderer getCellRenderer(int nIdx) { 081 switch (nIdx) { 082 case 1 : 083 return renderer; 084 } 085 return super.getCellRenderer(nIdx); 086 } 087 }